package com.societyGames.codeAlong.state.codeExercise.view.status
{
import com.societyGames.flashForms.CropContainer;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.getTimer;

/**
 * The status bar that appears below a mission module on complete, syntax error, or exception.
 **/
public class StatusBar extends Sprite
{
  private var _desiredWidth:Number;
  private var _desiredHeight:Number;
  private var _cropContainer:CropContainer = new CropContainer();
  private var _milliseconds:int; //How long to show the animation?
  private var _startTime:int;
  private var _startY:int; //Where did we start?

  public function StatusBar()
  {
    addChild(this._cropContainer);
  }

  public function get item():DisplayObject
  {
    return this._cropContainer.item;
  }

  override public function set width(value:Number):void
  {
    this._desiredWidth = value;
    recalculate();
  }

  override public function set height(value:Number):void
  {
    this._desiredHeight = value;
    recalculate();
  }

  private function recalculate():void
  {
    this._cropContainer.width = this._desiredWidth;
    this._cropContainer.height = this._desiredHeight;
    recalculateItem();
  }

  private function recalculateItem():void
  {
    if (this._cropContainer.item != null)
    {
      this._cropContainer.item.width = this._desiredWidth;
      this._cropContainer.item.height = this._desiredHeight;
    }
  }

  public function show(item:DisplayObject, milliseconds:int):void
  {
    this._cropContainer.item = item;
    recalculateItem();
    startAnimation(milliseconds);
  }

  private function startAnimation(milliseconds:int):void
  {
    if (this._cropContainer.item != null)
    {
      if (milliseconds <= 0)
      {
        completeAnimation();
      }
      else
      {
        this._milliseconds = milliseconds;
        //Start object at its height.
        this._startY = -this._cropContainer.item.height;
        this._cropContainer.item.y = this._startY;
        this._startTime = getTimer();
        this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
      }
    }
  }

  public function enterFrameHandler(event:Event):void
  {
    if (this._cropContainer.item != null) //If we still have a child...
    {
      var currentTime:int = getTimer();
      var percent:Number = (currentTime - this._startTime) / this._milliseconds;
      if (percent >= 1)
      {
        this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
        completeAnimation();
      }
      else
      {
        this._cropContainer.item.y = this._startY + (percent * this._cropContainer.item.height);
      }
    }
    else
    {
      this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }
  }

  private function completeAnimation():void
  {
    this._cropContainer.item.y = 0;
  }

  public function hide():void
  {
    this._cropContainer.item = null;
  }
}
}